var searchUrl = "http://127.0.0.1:9200/";
var infoUrl= "http://127.0.0.1:8085/";

//获取url中的参数
function getUrlParam(name) {
    //构造一个含有目标参数的正则表达式对象
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    //匹配目标参数
    var r = window.location.search.substr(1).match(reg);
//返回参数值
    if (r != null) return unescape(r[2]); return null;
}

function doSearch(type) {
    var search = document.getElementById("search").value;
    var photoUrl = ["https://ws1.sinaimg.cn/large/866fadacgy1fncgdl9l1ej207g066749.jpg",
        "https://ws1.sinaimg.cn/large/866fadacgy1fncgdl9toyj207g066jrl.jpg",
        "https://ws1.sinaimg.cn/large/866fadacgy1fncgdl8h3nj207g0660sm.jpg",
        "https://ws1.sinaimg.cn/large/866fadacgy1fncgdl9mxgj207g0660sm.jpg",
        "https://ws1.sinaimg.cn/large/866fadacgy1fncgdlamkvj207g0663yl.jpg"];

    if (search != "") {
        $.ajax({
            type: "post",
            // url: 'http://111.230.175.197:9200/project/_search',
            url: searchUrl + (type == 'project' ? 'c/' : 'z/') + type + '/_search',
            async: false,
            data: JSON.stringify({
                "query": {"match": {"full": search}}
            }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                // alert(data);
                var hitsNumber = data.hits.total;
                console.log(hitsNumber);

                var hits = data.hits.hits;
                console.log(hits);

                var searchList = document.getElementById("searchList");
                searchList.innerHTML = "";
                if (hitsNumber == 0) {
                    searchList.innerHTML = "<span style='align: center'> 什么都没有找到 </span>";
                    return;
                }

                for (var i = 0; type == 'project' && i < hitsNumber; i++) {
                // <li class="respl-item category-10">
                        // <div class="unit">
                        // <p class="hoverline"></p>
                        // <a class="images" href="http://www.17sucai.com/"><img src="images/upload/g1.jpg"></a>
                        // <h1 id="name">name</h1>
                        // <dl>
                        // <p id="field" >field</p>
                        // <p id="introProject">introProject</p>
                        // <p id="contactProject">contactProject</p>
                        // </dl>
                        // <div class="view">
                        // <a href=# onclick="javascript:addFavorite2()" class="left"><span>&lt;</span>加入收藏</a>
                        // <a class="right" href="">查看<span>&gt;</span></a>
                        // </div>
                        // </div>
                        // </li>
                    searchList.innerHTML +=
                        '<li class="respl-item category-10">' +
                        '<div class="unit">' +
                        '<p class="hoverline"></p>' +
                        '<a class="images" href="' + infoUrl + type + '/' + hits[i]._id + '"><img src="' + photoUrl[i] + '"></a>' +
                        '<h1 id="name">' + hits[i]._source.name + '</h1><dl>' +
                        '<p id="field" >'+ hits[i]._source.field + '</p>' +
                        '<p id="introProject">' + hits[i]._source.intro + '</p>' +
                        '</dl><div class="view">' +
                        '<a class="right" href="' +  infoUrl + type + '/' + hits[i]._id + '">查看<span>&gt;</span></a>' +
                        '</div></div></li>';

                }

                for (var i = 0; type == 'user' && i < hitsNumber; i++) {
                    // /<div class="col-xs-6 col-md-3 edit">
                    //     <a class="thumbnail" href="#">
                    //     <div style="float: left;color: black;">
                    //     <h6>张清媚</h6><br />
                    //     友汇网创始人，前阿里云..
                    // </div>
                    // <img src="img/p4.jpg" style="border-radius: 50%;width: 90px;height: 90px;margin:0px 0px 30px 170px;">
                    //     <div class="caption">
                    //     <h6 class="subhead">推荐指数4.6|满意度100%</h6>
                    // </div>
                    // </a>
                    // </div>
                    searchList.innerHTML +=
                        '<div class="col-xs-6 col-md-3 edit">'+
                        '<a class="thumbnail" href="' + infoUrl + type + '/' + hits[i]._id + '">'+
                        '<div style="float: left;color: black;">'+
                        '<h6>' + hits[i]._source.name + '</h6><br />' + hits[i]._source.intro+
                        '</div>'+
                        '<img src="/img/p7.jpg" style="border-radius: 50%;width: 90px;height: 90px;margin:0px 0px 30px 170px;">'+
                        '<div class="caption">'+
                        '<h6 class="subhead">'+ hits[i]._source.school + '|' + hits[i]._source.major + '</h6>'+
                        '</div></a></div>';

                }

            }

        });
    }
}